package com.sohu.peak.gallery.dao.hibernate;

import java.util.Date;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

import com.sohu.peak.gallery.dao.RecommendDAO;
import com.sohu.peak.gallery.domain.Recommend;
import com.sohu.peak.util.HibernateHelper;
import com.sohu.peak.util.Logger;
import com.sohu.peak.util.Pagination;

public class RecommendHibernateDAO extends BaseHibernateDAO implements RecommendDAO {
	static Logger log = new Logger();
	
	public boolean create(Recommend recommend) {
		try {
			recommend.setCreateTime(new Date());
			getSession().save(recommend);
			log.debug("save %s successful", recommend);
			return Boolean.TRUE;
		} catch (HibernateException re) {
			log.error("save %s HibernateException:%s", recommend, re);
		}
		return Boolean.FALSE;
	}

	public boolean update(Recommend recommend) {		
		try {
			getSession().update(recommend);
			log.debug("update %s successful", recommend);
			return Boolean.TRUE;
		} catch (HibernateException he) {
			log.error("update %s HibernateException:%s", recommend, he);
		}
		return Boolean.FALSE;
	}
	
	public boolean delete(Recommend recommend) {
		try {
			getSession().delete(recommend);
			log.debug("delete %s successful", recommend);
			return Boolean.TRUE;
		} catch (HibernateException he) {
			log.error("delete HibernateException:%s", he);
		}
		return Boolean.FALSE;
	}
	
	public Recommend read(Long id) {
		try {
			Recommend recommend = (Recommend) getSession().get(
					"com.sohu.peak.gallery.domain.Recommend", id);
			log.debug("read %s successful", id);
			return recommend;
		} catch (HibernateException he) {
			log.error("read HibernateException:%s", he);
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<Recommend> readAll(long cateId, Pagination pagination) {
		String queryStr = "from Recommend where cateId = ?";
		Session session = getSession();
		try {
			Query query = session.createQuery(queryStr.toString());
			List<Recommend> list = (List<Recommend>) HibernateHelper.pageQuery(session, query,
					pagination, new Object[] { cateId });
			log.debug("readAll %s successful, size:%s", cateId, list.size());
			return list;
		} catch (HibernateException he) {
			log.error("readAll cateId:%s, pagination:%s, HibernateException:%s", cateId, pagination, he);
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public List<Recommend> readVaild(long cateId, int type, int maxSize) {
		StringBuilder queryStr = new StringBuilder("select * from RECOMMENDS where CATE_ID = ? ")
								.append(" and TYPE = ? ")
								.append(" and START_TIME <= ? ")
								.append(" and END_TIME >= ? ")
								.append(" and DATE_ADD(CREATE_TIME, INTERVAL OVER_DAYS DAY) >= ? ");
		Date curDate = new Date();
		try {
			int i=0;
			Query query = getSession().createSQLQuery(queryStr.toString())
					.addEntity(Recommend.class)
					.setParameter(i++, cateId)
					.setParameter(i++, type)
					.setParameter(i++, curDate)
					.setParameter(i++, curDate)
					.setParameter(i++, curDate)						
					.setMaxResults(maxSize);
			List<Recommend> list = (List<Recommend>) query.list();
			log.debug("readVaild  cateId:%s, type:%s, maxSize:%s successful, size:%s", cateId, type, maxSize, list.size());
			return list;
		} catch (HibernateException he) {
			log.error("readVaild cateId:%s, type:%s, maxSize:%s, HibernateException:%s", cateId, type, maxSize, he.getMessage());
		}
		return null;
	}
}
